package com.haier.cloudremote;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;

import com.haier.cloudremote.common.SLog;
import com.haier.cloudremote.common.StaticData;
import com.haier.cloudremote.dialog.OnOkClickListener;
import com.haier.cloudremote.dialog.WhetherStudyDialog;
import com.haier.cloudremote.model.StbInfo;
import com.haier.cloudremote.model.StbList;
import com.haier.cloudremote.protocal.CAEProtocal;

import java.util.ArrayList;

public class GetStbLoadActivity extends Activity {
	 public static ArrayList<StbInfo> stbInfos=null;
	 private static final String TAG = "cloudremote";
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		 // 设置全屏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.loading);
		
		ActivityStackControlUtil.add(this); 

        int provinceId = StaticData.checkedProvince.getPid();
        int cityId = StaticData.checkedCity.getCityId();
        int townId = StaticData.checkedTown.getTownId();
        
		GetStbFromCAE getStbFromCAE = new GetStbFromCAE();
		getStbFromCAE.execute(provinceId,cityId,townId);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	class GetStbFromCAE extends AsyncTask<Integer,Integer,ArrayList<StbInfo>>{
	    
		@Override
        protected ArrayList<StbInfo> doInBackground(Integer... params) {
            int provinceId = params[0];
            int cityId = params[1];
            int townId = params[2];
            // TODO Auto-generated method stub
            SLog.d("ssc", "向CAE获取STB------------");
            StbList stblist = CAEProtocal.getStbListFromCAE(provinceId, cityId, townId, "");
           
            SLog.d("ssc", "stbInfos=="+stbInfos);
            if(stblist!=null){
                stbInfos = stblist.getStbInfos();
                if(stbInfos!=null && stbInfos.size()>0){
                	SLog.d("ssc", "获取到STB------------stbInfos=="+stbInfos.get(0).getStbName());
                }
                
            }
            return stbInfos;
        }

        @Override
        protected void onPostExecute(final ArrayList<StbInfo> stbInfos) {
            // TODO Auto-generated method stub
            super.onPostExecute(stbInfos);
			if(stbInfos!=null && stbInfos.size() > 0){
				SLog.d(TAG, "得倒的城市码表个数为======="+stbInfos.size());
            	final StbInfo sInfo = stbInfos.get(0);
            	WhetherStudyDialog whetherStudyDialog = new WhetherStudyDialog(GetStbLoadActivity.this);
            	whetherStudyDialog.setOnOkClickListener(new OnOkClickListener() {
					@Override
					public void onOkClick() {
						// TODO Auto-generated method stub
						useSTB(stbInfos);
					}
					
					@Override
					public void onCancelClick() {
						// TODO Auto-generated method stub
						studySTB();
					}
				});
            	whetherStudyDialog.show();
            	
            }else{
				SLog.d(TAG, "没得倒码表");
            	studySTB();
            }
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }
	    
	}
	
	/**
	 * 使用STB
	 * @param stbInfos2
	 */
	private void useSTB(ArrayList<StbInfo> stbInfos) {
		// TODO Auto-generated method stub
		if (stbInfos!=null && stbInfos.size() == 1) {
			Intent intent = new Intent();
			intent.setClass(GetStbLoadActivity.this, FinishActivity.class);
			SetStbThread setStbThread = new SetStbThread(stbInfos.get(0));
			setStbThread.start();
			startActivity(intent); 
			
		}else if (stbInfos!=null && stbInfos.size() > 0) {
			SLog.d(TAG, "得倒的城市码表个数为多个");
        	Intent intent = new Intent();
    		intent.setClass(GetStbLoadActivity.this, SelectTownActivity.class);
            startActivity(intent); 
		}
	}
	
	/**
	 * 手动学习STB
	 */
	private void studySTB() {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.setClass(GetStbLoadActivity.this, StartStudyActivity.class);
        startActivity(intent); 
	}
	
	class SetStbThread extends Thread{
	    StbInfo stbInfo;

        public SetStbThread(StbInfo stbInfo) {
            super();
            this.stbInfo = stbInfo;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            CAEProtocal.setStb(stbInfo);
        }
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		SLog.d("ssc", "GetStbLoadActivity-----------onPause()");
		finish();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		SLog.d("ssc", "GetStbLoadActivity-----------onStop()");
		
	}
	
	
}
